//Main extension variable
var gOneClickExtension = {};

(function(){
    var gAltClick    = false; //Last click in thread tree was with pressed "alt" key. Uses to prevent menu selection after click
    var gAltSelected = false; //Selection change of thread tree was done with pressed "alt" key. Uses to present unnessary message selections
    var gOldThreadPaneSelectionChanged = {}; //Previous handler of thread pane selection change

    //Clicked at message thread
    this.onAltClicked = function ( event )
    {
        if ( !event.altKey ) return;

        gAltClick = true;

        var row = {};
        var col = {};
        var childElt = {};
        event.currentTarget.treeBoxObject.getCellAt(event.clientX, event.clientY, row, col, childElt);

        var sCellText = event.currentTarget.view.getCellText(row.value, col.value);
        var aMenuItem = {};
        var searchMode;

        //only subject, sender and recipient columns are supported
        if ( col.value.id == "subjectCol" )
            searchMode = 0;
        else if ( col.value.id == "senderCol" )
            searchMode = 1;
        else if ( col.value.id == "recipientCol" )
            searchMode = 5;
        else 
            return;
        
        document.getElementById("searchInput").value = sCellText;
        document.getElementById("searchInput").clearButtonHidden = false;

        aMenuItem.value = searchMode; //sender
        gSearchInput.showingSearchCriteria = false;
        changeQuickSearchMode(aMenuItem);
        onEnterInSearchBar();

        event.preventDefault();
        event.stopPropagation();
    }

    //Pressed some key when focus is at quick search panel
    this.onSearchKeyPress = function (event)
    {
        // 27 == esc
        if (event && event.keyCode == 27 && gSearchInput.value != "" )
            onClearSearch();
    }

    //1) Prevent main menu activation after "alt" key press
	//2) Clear search if "esc" is pressed
    this.onWindowKeyUp = function (event)
    {
		//we hold on "alt" and clicked at message -> we are going to search messages, do not select menu
        if ( gAltClick && event.altKey == false && event.keyCode == 18 )
        {
            gAltClick = false;
            event.stopPropagation();
            return;
        }

        if ( event.keyCode == 27 && gSearchInput.value != "" )
        {
            onClearSearch();
        }
    }

    //when we alt+clicked at message, it changes selection. But we simply want to search, so we prevent selection change
    this.onThreadPaneSelectionChanged = function (event)
    {
        if (!gAltSelected)
            gOldThreadPaneSelectionChanged(event);
        else
            gAltSelected = false;
    }

    //To understand, when we alt+click at message
    this.onThreadPaneSelectionMousedown = function (event)
    {
        if (event.altKey) gAltSelected = true;
    }

    //Add handlers
    this.Initialize = function() {
        
        window.addEventListener("keyup", gOneClickExtension.onWindowKeyUp, true);

        document.getElementById("threadTree").addEventListener("click",     gOneClickExtension.onAltClicked,                   true);
        document.getElementById("threadTree").addEventListener("mousedown", gOneClickExtension.onThreadPaneSelectionMousedown, true);

        gOldThreadPaneSelectionChanged = document.getElementById("threadTree").onselect;
        document.getElementById("threadTree").onselect = gOneClickExtension.onThreadPaneSelectionChanged;

        document.getElementById("searchInput").addEventListener("keypress", gOneClickExtension.onSearchKeyPress,               true);
        
    };
    // Clean up
    this.Shutdown = function() {
        window.removeEventListener("load", gOneClickExtension.Initialize, false);
        window.removeEventListener("unload", gOneClickExtension.Shutdown, false);

        window.removeEventListener("keyup", gOneClickExtension.onWindowKeyUp, true);

        document.getElementById("threadTree").removeEventListener("click",     gOneClickExtension.onAltClicked,                   true);
        document.getElementById("threadTree").removeEventListener("mousedown", gOneClickExtension.onThreadPaneSelectionMousedown, true);
        document.getElementById("searchInput").removeEventListener("keypress", gOneClickExtension.onSearchKeyPress,               true);

        document.getElementById("threadTree").onselect = gOldThreadPaneSelectionChanged;
    };

    window.addEventListener("load", gOneClickExtension.Initialize, false);
    window.addEventListener("unload", gOneClickExtension.Shutdown, false);

}).apply(gOneClickExtension);